package org.streets.eis.ext.rbac.component;

import java.util.List;

import javax.swing.tree.DefaultTreeModel;

import org.apache.wicket.util.string.Strings;
import org.streets.database.SQLConnection;
import org.streets.eis.EisWebSession;
import org.streets.eis.component.model.MenuItem;
import org.streets.eis.component.tree.Node;
import org.streets.eis.component.tree.TreePanel;
import org.streets.eis.entity.User;
import org.streets.eis.ext.rbac.services.RBACAuthorizer;
import org.streets.context.definition.ContextDefinition;
import org.streets.context.definition.FunctionDefinition;
import org.streets.context.definition.ModuleDefinition;
import org.streets.wicket.HiveWebApplication;

public class ModuleTree extends TreePanel<MenuItem>{

	private static final long serialVersionUID = 1L;
	
	public static final MenuItem ROOT = new MenuItem();
	
	public ModuleTree(String id) {
		this(id,false);
	}

	public ModuleTree(String id,boolean showCheckBox){
		super(id,(DefaultTreeModel)null,showCheckBox);
		
		Node<MenuItem> rootNode = new Node<MenuItem>(ROOT);
		
		SQLConnection conn = HiveWebApplication.get().getRegistry().service(SQLConnection.class);
		User user = EisWebSession.get().getUser();
		RBACAuthorizer authorizer = HiveWebApplication.get().getRegistry().service(RBACAuthorizer.class);
		
		ContextDefinition definition = HiveWebApplication.get().getRegistry().service(ContextDefinition.class);
		List<ModuleDefinition> mds = definition.getModules();
		for (ModuleDefinition md : mds) { 			
			if (md.getType() != 1) continue;
			generateModule(rootNode, md,conn,authorizer,user);
		}
		
		DefaultTreeModel model = new DefaultTreeModel(rootNode);
		getTree().setDefaultModelObject(model);
		getTree().setRootLess(true); 
	}

	protected void generateModule(Node<MenuItem> parentNode, ModuleDefinition def, SQLConnection conn,RBACAuthorizer authorizer,User user) {
		parentNode.setLoaded(true);		
		if (Strings.isEmpty(def.getName()) || "-".equals(def.getName())) return;
		
		String expr = "func:"+def.getId();
		if (!authorizer.authorized(user, expr)) return;
		
		Node<MenuItem> node = new Node<MenuItem>(new MenuItem(def.getId(),"", def.getName(), def.getPortal(),def.getHint()).setIcon(def.getIcon()));
		node.setLoaded(true);
		parentNode.add(node);
		
		//添加功能项
		List<FunctionDefinition> funcs = def.getFunctions();
		for (FunctionDefinition func: funcs) {
			if (Strings.isEmpty(func.getName()) || "-".equals(func.getName())) continue;
			expr = "func:"+func.getId();
			if (!authorizer.authorized(user, expr)) continue;
			
			Node<MenuItem> tmp = new Node<MenuItem>(new MenuItem(func.getId(),"", func.getName(), func.getPortal(),func.getHint()).setIcon(def.getIcon()));
			tmp.setLoaded(true);
			node.add(tmp);
		}
		//添加子菜单
		List<ModuleDefinition> mds = def.getSubModules();
		for (ModuleDefinition md : mds) {
			if (md.getType() != 1) continue;
			generateModule(node, md,conn,authorizer,user);
		}
		
	}

}
